﻿using ReactiveUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _02_WimForms_Bindings
{
    public partial class MainView : Form, IViewFor<MainViewModel>
    {
        //对象的值
        public MainViewModel ViewModel { get; set; }
        //设置主窗体的绑定响应对象
        object IViewFor.ViewModel
        { 
            get => ViewModel;
            set => ViewModel=(MainViewModel)value;
        }
        public MainView()
        {
            InitializeComponent();

            this.WhenActivated(d => 
            {
                //从窗口模型(Model)到窗口(Form)的单向绑定
                d(this.OneWayBind(ViewModel, vm => vm.ApplicationTitle, v => v.Text));
                //输入的双向绑定（文本框）和输出的单向绑定（标签）
                d(this.Bind(ViewModel, vm => vm.ValueOne, v => v.tbInputOne.Text));//文本框
                d(this.OneWayBind(ViewModel, vm => vm.ValueOne, v => v.lOutputOne.Text));//标签

                //带转换的双向输入绑定（文本框）和单向输出绑定（标签）
                d(this.Bind(ViewModel, vm => vm.ValueTwo, v => v.dtpInputTwo.Value, t => DateTime.FromFileTime(t), dt => dt.ToFileTime()));
                d(this.OneWayBind(ViewModel, vm => vm.ValueTwo, v => v.lOutputTwo.Text));
            });

            //设置属性
            ViewModel = new MainViewModel();
        }

    }
}
